﻿using System;
using Birkelund.QueryStrings.Tokens;
using NUnit.Framework;
using System.Web;

namespace Birkelund.QueryStrings.Test
{
    public class StringTokenTest
    {
        [Test]
        public void ReadFailureAction_ValidationError()
        {
            StringToken token = new StringToken("a")
            {
                Validator = (input) => input.Length == 5,
                ExceptionNotificationBehaviour = ExceptionNotificationBehaviour.Silent
            };

            bool isEventFired = false;
            token.ReadFailureAction = (args) =>
            {
                Assert.AreEqual(args.Reason, FailureReason.ValidationError);
                Assert.AreEqual(args.FullUri, "http://www.test.dk/test.html?a=huhu");
                Assert.AreEqual(args.Value, "huhu");
                Assert.AreEqual(args.KeyName, "a");
                Assert.IsNull(args.Exception);
                isEventFired = true;
            };

            token.ReadValue(new Uri("http://www.test.dk/test.html?a=huhu"));
            Assert.IsTrue(isEventFired);
        }

        [Test]
        public void ReadFailureAction_TokenDoesNotExist()
        {
            StringToken token = new StringToken("a")
            {
                DefaultValue = "myDefault",
                Validator = (input) => input.Length == 5,
                ExceptionNotificationBehaviour = ExceptionNotificationBehaviour.Silent
            };

            bool isEventFired = false;
            token.ReadFailureAction = (args) =>
            {
                Assert.AreEqual(args.Reason, FailureReason.TokenDoesNotExist);
                Assert.AreEqual(args.FullUri, "http://www.test.dk/test.html?");
                Assert.IsNull(args.Value);
                Assert.AreEqual(args.KeyName, "a");
                Assert.IsNull(args.Exception);
                isEventFired = true;
            };

            token.ReadValue(new Uri("http://www.test.dk/test.html?"));
            Assert.IsTrue(isEventFired);
        }

        [Test]
        public void ReadTokenValue_Null()
        {
            StringToken a = new StringToken("a");
            string initialUrl = "http://localhost/test.aspx";

            string value = a.ReadNullIfEmpty(new Uri(initialUrl));
            Assert.IsNull(value);
        }

        [Test]
        public void DefaultValue_Null()
        {
            StringToken a = new StringToken("a");
            string expected = null;

            Assert.AreEqual(expected, a.DefaultValue);
        }

        [Test]
        public void DefaultValue()
        {
            StringToken a = new StringToken("a")
            {
                DefaultValue = "myDefault"
            };
            string expected = "myDefault";

            Assert.AreEqual(expected, a.DefaultValue);
        }

        [Test]
        public void RenderTokenValue_Simple()
        {
            StringToken a = new StringToken("a");

            string expected = "myvalue";

            Assert.AreEqual(expected, a.RenderTokenValue("myvalue"));
        }


        [Test]
        public void RenderTokenValue_WithSpecialChars()
        {
            string valueToTest = @"\æ &""?øå'%++´`=¨";
            StringToken a = new StringToken("a");

            string expected = HttpUtility.UrlEncode(valueToTest);

            Assert.AreEqual(expected, a.RenderTokenValue(valueToTest));
        }

        [Test]
        public void RenderTokenValue_Null()
        {
            StringToken a = new StringToken("a") { RenderIfEmptyValue = true };
            string expected = string.Empty;

            Assert.AreEqual(expected, a.RenderTokenValue(null));
        }
    }
}
